package cn.hs.controller;

import cn.hs.entity.Heartknowledge;
import cn.hs.service.HeartknowledgeService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName HeartknowledgeController
 * @Description TODO
 * @Author yyk
 * @Date 2024年10月23日 14:23
 * @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/HeartknowledgeController")
public class HeartknowledgeController {
    @Autowired
    private HeartknowledgeService heartknowledgeService;
    @GetMapping("/findAll")
    public Result findAll(String name, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize) {
        Heartknowledge heartknowledge = new Heartknowledge(name);

        PageInfo all = heartknowledgeService.findAll(heartknowledge, pageNum, pageSize);
        if (all!=null && all.getTotal()>0) {
            return Result.success(all);
        }else {
            return Result.error();
        }
    }

    @PostMapping("/insert")
    public Result insert( @RequestBody Heartknowledge heartknowledge) {
        int insert = heartknowledgeService.insert(heartknowledge);
        if (insert>0) {
            return Result.success(insert);
        }else {
            return Result.error();
        }
    }
@PostMapping("/updateByPrimaryKeySelective")
    public Result updateByPrimaryKeySelective(@RequestBody Heartknowledge heartknowledge) {
        int update = heartknowledgeService.updateByPrimaryKeySelective(heartknowledge);
        if (update>0) {
            return Result.success(update);
        }else {
            return Result.error();
        }
}
@PostMapping("/updateByPrimaryKey")
    public Result updateByPrimaryKey(@RequestBody Heartknowledge heartknowledge) {
        int update = heartknowledgeService.updateByPrimaryKey(heartknowledge);
        if (update>0) {
            return Result.success(update);
        }else {
            return Result.error();
        }
}

    @GetMapping("/deleteByPrimaryKey")
    public Result deleteByPrimaryKey(@RequestParam Integer id) {
        int delete = heartknowledgeService.deleteByPrimaryKey(id);
        if (delete>0) {
            return Result.success(delete);
        }else {
            return Result.error();
        }
}

        @GetMapping("findById")
    public Result findById(@RequestParam Integer id){
            Heartknowledge byId = heartknowledgeService.findById(id);
            return Result.success(byId);
        }



}
